define(function(require, exports, module) {

	var $ = require('jquery');
	var List = require('../util/list');
	require('jbox');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-json/2.4.0/jquery.json.js');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('../util/getURL.js');

	var uid = '';
	if($g_user.login == 'true'){
		uid = ''+$g_user.id;
	}else{
		uid = '-1';
	}
	
	if(uid=="-1"){
		var submit = function (v, h, f) {
		    if (v == 'ok')
		    	window.location.href=$CONFIG.base_url +"/login";
		};
		$.jBox.confirm("登录超时，请点击确定重新登录？", "<span style='text-align:center;width:100%;display:block;color: #834313;'>友情提示</span>", submit);
	}

	var path = window.location.href;
	var aid;
	if(path.lastIndexOf('?')!=-1){	
		aid = path.substring(path.lastIndexOf('?') + 4);
		if(isNaN(aid)){
			aid=aid.substring(path.lastIndexOf('?') + 2,path.length);
		}
	}else{
		aid=$g_user.id;
	}
	
	if((aid!=-1)&&(aid!=$g_user.id)){
		$("#renzhengjigou").css("display","none");
		$("#renzhengzrj").css("display","none");
	}
	$.ajaxSetup({
		cache : false
	});

	$('#home-tab a').click(function(e) {
		e.preventDefault();
		$(this).tab('show');
	});
	module.exports = List;
	function List() {
		this.options = [];
	}
	;
	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : '',
	};
	
	
	var status='';
	switch($g_user.role){
		case 'user':
		status='3';
		break;
		case 'institution':
		status='-1';
		break;
		default:
		status='3';
	}
	
	var uid = $g_user.id;
	var userId = UrlParm.parm("id");
	if (uid == userId) {
		id = uid;
		$(".chart_button").remove();		
	} else if (userId == undefined) {
		id = uid;
		$(".chart_button").remove();
	} else {
		id = userId;
		$(".friendId").val(userId);
		$(".userId").val(uid);
		$(".chart_button").show();
		/*if($g_user.role=="'charitarian'"){
			status='2';
		}else{			
			status='3';
		}*/
	}

	var userName = '';

	//时间戳转换成日期
	exports.getLocalTime = function(now) {
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var date = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		var second = now.getSeconds();
		return year + "-" + month + "-" + date + " " + hour + ":" + minute
				+ ":" + second;
	};
	exports.format = function(e) {
		$("#" + e + " .commment_creatdate").each(function() {
			var t = parseInt($(this).text());
			var d = new Date(t);
			t = exports.getLocalTime(d);
			$(this).text(t);
		});
	};
	
	$(".chart_button").click(function(){
		$('#send-name').text($("#span_name").text());
	});
	// 验证
	$("#sendLeterTo").validate({
		rules : {
			content : {
				required : true
			},
		}
	});
	// 用户部分信息
	exports.options.userLinks = $CONFIG.base_url + "/api/user";
	exports.options.userParams = {
		"uid" : aid
	};

	$.get($CONFIG.base_url +"/api/user/check",{"uid":uid})
	.done(function(data){
		if(data == 0)
			$("#renzhengjigou").text("认证个人服务商");
		else if(data.status == 1)
			$("#renzhengjigou").text("认证审核中...");
		else if(data.status == 3)
			$("#renzhengjigou").text("重新认证个人服务商");
	})


	// //exports.checkUser = function checkUser(url){
	// 	var id=$("#user_id").val();
	// 	$.get($CONFIG.base_url+"/api/user/check?uid="+id,function(data){

	// 		if(data.status=="1"){
	// 			$("#renzhengjigou").text("审核中...");
	// 		}else if(data.status==2){
	// 			$("#renzhengjigou").hide();
	// 		}else {
	// 			$("#renzhengjigou").text("认证个人服务商");
	// 		}
	// 	});
	// //};




	var userList = $.get(exports.options.userLinks, exports.options.userParams);
	userList.done(function(data) {
		var selectKind = template.render('userInfor-template', data);
		$('#userInfor').empty().append(selectKind);
		roles = data.user.roles;
		switch (roles) {
		case "user":
			$("#roles").text("普通用户");
			$("#userTitle").after(
					"<div id='web'>微博或博客：" + data.weiboUrl + "</div>");
			$("#supportProject").addClass("active");
			$("#support-project").addClass("active");
			break;
		case "institution":
			$("#roles").text("个人服务商");
			$("#startedProject").show();
			$("#start-project").show();
			start.load();
			break;
		default:
			window.location.href=$CONFIG.base_url+"/"+$CONFIG.admin_url;
		}
		userName = data.name;
		$('.send_to').text(userName);
	});
	console.log(status);
	console.log(id);
	// 我发起的项目
	var start = new List();
	start.options.template = "start-project-template";
	start.options.container = "#start-project";
	start.options.links = {
		"list" : $CONFIG.base_url + "/api/project/start"
	};
	start.options.params = {
		"uid" : id,
		"status":status,
		"p" : "1",
		"s" : "4"
	};
	/*// 我推荐的项目
	var recommend = new List();
	recommend.options.template = "recommend-project-template";
	recommend.options.container = "#recommend-project";
	recommend.options.links = {
		"list" : $CONFIG.base_url + "/api/project/recommends"
	};
	recommend.options.params = {
		"uid" : id,
		"status":status,
		"p" : "1",
		"s" : "4"
	};
	recommend.load();*/
	// 我资助的项目
	var support = new List();
	support.options.template = "support-project-template";
	support.options.container = "#support-project";
	support.options.links = {
		"list" : $CONFIG.base_url + "/api/project/support/user/list"
	};
	support.options.params = {
		"uid" : id,
		"status":status,
		"p" : "1",
		"s" : "4"
	};
	support.load(
			);
	// 我分享的项目
	var share = new List();
	share.options.template = "share-project-template";
	share.options.container = "#share-project";
	share.options.links = {
		"list" : $CONFIG.base_url + "/api/project/share/user/list"
	};
	share.options.params = {
		"uid" : id,
		"status":status,
		"p" : "1",
		"s" : "4"
	};
	share.load();
	// 我关注的项目
	var attention = new List();
	attention.options.template = "attention-project-template";
	attention.options.container = "#attention-project";
	attention.options.links = {
		"list" : $CONFIG.base_url + "/api/project/attention/user/list"
	};
	attention.options.params = {
		"uid" : id,
		"status":status,
		"p" : "1",
		"s" : "4"
	};
	attention.load();
	// 我评论的项目
	var comment = new List();
	comment.options.template = "commend-project-template";
	comment.options.container = "#comment-project";
	comment.options.links = {"list" : $CONFIG.base_url + "/api/comment/mycomment"};
	comment.options.params = {
		"uid" : id,
		"status":status,
		"p" : "1",
		"s" : "4"
	};
	comment.load(function(){
//		$("#comment-project .commment_creatdate").each(function(){
//			var s_d=$(this).text();
//			var new_date=exports.getLocalTime(s_d);
//			$(this).text(new_date);
//		});
		exports.format("comment-project");
	});

	// 定时器
	var secs = 1;
	exports.auto_jump = function auto_jump() {
		secs = secs - 1;
		if (secs < 0) {
			return false;
		}
		if (secs == 0) {
			clearInterval(time);
			$(".letter_send_seccuss").hide();
		}
	};
	// 提交表单
	exports.submitForm = function submitForm(e, v) {
		var json = form2js(e, '.', true);
		var postData = $.toJSON(json);
		//console.log(postData);
		$.ajax({	url : $CONFIG.base_url + v,
					type : "POST",
					data : postData,
					// dataType : "json",
					contentType : "application/json",
					success : function(data) {
						$.jBox.closeTip();
						$.jBox.tip("发送成功！");
						$("#home .close").click();
					},
					error : function() {
						$("#chart .chart_submit label").removeClass("label");
						$(".chart_submit label").text("发送");
						$(".chart_inform")
								.after(
										"<span class='send_error'></span><span style='color:red;hight:30px;line-height:30px;'>由于网络原因你的私信没有发送成功，请重新发送。</span>");
					}
				});
	};
	// 发送私信
	$("#chartSubmit").click(function() {
		$this = $("#sendLeterTo");
		if (!$this.valid()) {
			return;
		}
		$.jBox.tip("发送中...","loading");
		exports.submitForm(sendLeterTo, "/api/letter/send");
	});
	$("#renzhengjigou").click(function(){
		
		exports.checkUser($CONFIG.base_url+ "/verify/orgapply");
	});
	$("#renzhengzrj").click(function(){
		exports.checkUser($CONFIG.base_url + "/verify/fameapply");
	});
	
	exports.checkUser = function checkUser(url){
		var id=$("#user_id").val();
		$.get($CONFIG.base_url+"/api/user/check?uid="+id,function(data){
			if(data.status=="1"){
				$.jBox.tip("您认证的个人服务商正在处理中，不能再认证了");
				return false;
			}else{
				window.open( url,"_self");
			}
		});
	};
	$.get($CONFIG.base_url+"/api/user",{"uid":$g_user.id}).done(function(){
		var myName=$(".loginna").attr("data");
		if($g_user.id!=myName){
			$("#gyjsz").hide();
		};
	});
	
});
